<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="rw.ac.kiosc.jdbc.school.orm.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.HashSet" %>
<%@ page import="rw.ac.kiosc.jdbc.school.School" %>
<%@ page import="rw.ac.kiosc.jdbc.school.orm.Department" %>
<%@ page import="rw.ac.kiosc.jdbc.school.orm.Student" %>
<%
            School school = new School();
            User user = null;
            if (session == null) {
                response.sendRedirect("login.htm");
            }
            String userIp = (String) session.getAttribute("userIp");
            int id = 0;
            String userId = (String) session.getAttribute("userId");
            if (userIp != null && userId != null) {
                try {
                    id = Integer.parseInt(userId);
                } catch (Exception ex) {
                    session.invalidate();
                    response.sendRedirect("login.htm");
                }
                if (userIp.equals(request.getRemoteAddr())) {
                    user = school.getUserDAO().getUserById(id);
                    if (user == null) {
                        session.invalidate();
                        response.sendRedirect("login.htm");
                    }
                } else {
                    session.invalidate();
                    response.sendRedirect("login.htm");
                }
            } else {
                session.invalidate();
                response.sendRedirect("login.htm");
            }

            if ("POST".equals(request.getMethod())) {
                String acronym = request.getParameter("acronym");
                int yos = 0;
                try {
                    yos = Integer.parseInt(request.getParameter("yos").replace(" ", ""));
                } catch (Exception ex) {
                    ex.printStackTrace();
                    System.out.println(ex.toString());
                }
                String name = request.getParameter("name");

                Department department = new Department(acronym, yos, name, new HashSet<Student>());
                school.getDepartmentDAO().saveDepartment(department);
            }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>List of Departments</title>
    </head>
    <body>
        <%@ include file="menu.jsp" %>
        <h3>List of Departments</h3>
        <table border="1">
            <tbody>
                <tr>
                    <td>Department ID</td>
                    <td>Acronym</td>
                    <td>Name</td>
                    <td>Number of Years</td>
                    <td>Number of Students</td>
                </tr>
                <%
            List<Department> departments = school.getDepartmentDAO().getAllDepartments();
            for (Department department : departments) {
                %>
                <tr>
                    <td><%=department.getDepartmentId()%></td>
                    <td><%=department.getAcronym()%></td>
                    <td><%=department.getName()%></td>
                    <td><%=department.getNumberOfYears()%></td>
                    <td><%=department.getStudents().size()%></td>
                </tr>
                <%
            }
                %>
                <tr>
            <form method="post" action="departments.htm">
                <td><input type="submit" value="Add new"/></td>
                <td><input type="text" name="acronym" id="acronym"/></td>
                <td><input type="text" name="name" id="name"/></td>
                <td><input type="text" name="yos" id="yos"/></td>
                <td>&nbsp;</td>
            </form>
        </tr>
    </tbody>
</table>
</body>
</html>

